# -*- coding:utf-8 -*-
import xlrd
import json
import os
from datetime import datetime
from xlrd import xldate_as_tuple

PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')).replace('\\', '/')


class GetCaseData:
    """获取excel数据"""

    def __init__(self, file_path):
        self.file_path = file_path
        self.workbook = xlrd.open_workbook(self.file_path)

    def get_run_config(self):
        """ 获取config.json中的配置 """

        with open(PATH + '/dataconfig/config.json', "r", encoding='utf-8') as f:
            conf = json.load(f)

        return conf

    def get_common_data(self):
        """ 获取接口数据表的"config"sheet页中定义的测试环境、登录接口、登录参数；获取运行产品ID；返回dict """

        config = {}

        # projectid = self.file_path.split('interface-')[1].split('.')[0]
        # config['projectid'] = projectid

        config_sheet = self.workbook.sheet_by_name('config')
        config['environment'] = config_sheet.cell_value(0, 1)
        config['login_interface'] = config_sheet.cell_value(1, 1)
        config['login_param'] = config_sheet.cell_value(2, 1)

        return config

    def get_case(self, caseid):
        """获取case信息"""

        case = {}

        for sheet in self.workbook.sheets():
            if sheet.nrows != 1 and sheet.name != 'config':
                for row in (1, sheet.nrows):
                    if str(int(sheet.cell_value(row, 0))) == caseid:
                        case['sheet_name'] = sheet.name
                        case['row'] = row
                        case['case_id'] = int(sheet.cell_value(row, 0))
                        case['case_name'] = sheet.cell_value(row, 1)
                        case['case_url'] = sheet.cell_value(row, 2)
                        case['case_method'] = sheet.cell_value(row, 3)
                        case['case_isrun'] = sheet.cell_value(row, 4)
                        depend = sheet.cell_value(row, 5)
                        if sheet.cell(row, 5).ctype == 2:
                            case['case_depend'] = str(int(depend))
                        else:
                            case['case_depend'] = depend
                        case['case_dependparam'] = sheet.cell_value(row, 6)
                        case['case_dependway'] = sheet.cell_value(row, 7)
                        case['case_param'] = sheet.cell_value(row, 8)
                        case['case_expect'] = sheet.cell_value(row, 9)
                        case['case_actual'] = sheet.cell_value(row, 10)
                        case['case_result'] = sheet.cell_value(row, 11)
                        case['case_exectime'] = sheet.cell_value(row, 12)

                        return case
        else:
            print("没有找到用例，请检查测试数据。")
            return case

    def get_depend_data(self, caseid):
        """ 获取依赖用例的详细数据数据
        返回dict:case_depend、case_dependway、case_dependdata
        """

        data = {}

        case = self.get_case(caseid)
        depend_case = self.get_case(case['case_depend'])

        data['case_depend'] = case['case_depend']
        data['case_dependparam'] = case['case_dependparam']
        data['case_dependway'] = case['case_dependway']
        depend_reponse = json.load(depend_case['case_actual'])

        dependparam_list = data['case_dependparam'].split(',')
        if len(dependparam_list) == 3:
            if dependparam_list[1].isdigit():
                response_key = dependparam_list[0]
                response_value_index = int(dependparam_list[1])
                response_value_key = dependparam_list[3]
                depend_data = depend_reponse.get(response_key)[response_value_index]\
                    .get(response_value_key)
            else:
                case['case_actual'] = 'FAILED'
                case['case_result'] = '依赖参数有问题，无法获取依赖用例对应的参数值，请检查数据。'
        elif len(dependparam_list) == 1:
            response_key = dependparam_list[0]
            depend_data = depend_reponse.get(response_key)
        else:
            case['case_actual'] = 'FAILED'
            case['case_result'] = '依赖参数有问题，无法获取依赖用例对应的参数值，请检查数据。'

        data['case_dependdata'] = depend_data

        return data



